package com.whlilong.jxc.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.whlilong.jxc.service.db.IConnection;

@SuppressWarnings("unchecked")
public class ConnectImpl implements IConnection {

	@Override
	public int delete(Object obj) {
		Session session = HibernateSessionFactory
				.getSession("hibernate_derby.cfg.xml");
		Transaction tx = session.beginTransaction();
		session.delete(obj);
		tx.commit();
		HibernateSessionFactory.closeSession();
		return 0;
	}

	@Override
	public int insert(Object obj) {
		Session session = HibernateSessionFactory
				.getSession("hibernate_derby.cfg.xml");
		Transaction tx = session.beginTransaction();
		session.save(obj);
		tx.commit();
		HibernateSessionFactory.closeSession();
		return 0;
	}

	@Override
	public List search(Object obj) {
		Session session = HibernateSessionFactory
				.getSession("hibernate_derby.cfg.xml");
		Query query = session.getNamedQuery(obj.toString());
		/*
		 * ScrollableResults result =
		 * query.setCacheMode(CacheMode.IGNORE).scroll(
		 * ScrollMode.FORWARD_ONLY);
		 */
		List list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}

	public List search(String sql, int[] parameter, Object[] values) {
		Session session = HibernateSessionFactory
				.getSession("hibernate_derby.cfg.xml");
		Query query = session.getNamedQuery(sql);
		if (parameter != null)
			for (int i = 0; i < parameter.length; i++) {
				query = query.setParameter(parameter[i], values[i]);
			}
		List list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}

	@Override
	public int update(Object obj) {
		Session session = HibernateSessionFactory
				.getSession("hibernate_derby.cfg.xml");
		Transaction tx = session.beginTransaction();
		session.update(obj);
		tx.commit();
		HibernateSessionFactory.closeSession();
		return 0;
	}

	@Override
	public List search(String sql, Integer[] parameter, Object[] values) {
		Session session = HibernateSessionFactory
				.getSession("hibernate_derby.cfg.xml");
		Query query = session.createQuery(sql);
		if (parameter != null)
			for (int i = 0; i < parameter.length; i++) {
				query = query.setParameter(parameter[i], values[i]);
			}
		List list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}

	@Override
	public int create(String sql) {
		// TODO Auto-generated method stub
		Session session = HibernateSessionFactory
				.getSession("hibernate_derby.cfg.xml");
		Transaction tx = session.beginTransaction();
		Connection conn = session.connection();
		Statement st;
		try {
			st = conn.createStatement();
			st.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		tx.commit();
		HibernateSessionFactory.closeSession();
		return 0;
	}

}
